﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Porter
{
    public class FavoriteStore
    {
        private string filepath;
        public bool Valid { get; private set; }

        [Serializable]
        public class Segment
        {
            [XmlAttribute]
            public int stIdBegin;
            [XmlAttribute]
            public int stIdEnd;
            [XmlAttribute]
            public int lnId;
            [XmlAttribute]
            public string lnName;
        }

        [Serializable]
        public class Route
        {
            [XmlAttribute]
            public string Name;
            [XmlArray]
            public List<Service.Station> Stations;
            [XmlElement]
            public List<Segment> Segments;

            public Route()
            {
                Stations = new List<Service.Station>();
                Segments = new List<Segment>();
            }
        }

        [Serializable]
        [XmlRoot]
        public class Favorites
        {
            [XmlElement]
            public List<Service.Line> Lines;
            [XmlElement]
            public List<Route> Routes;
            [XmlElement]
            public List<Map.GeoArea> Areas;

            public Favorites()
            {
                Lines = new List<Service.Line>();
                Routes = new List<Route>();
                Areas = new List<Map.GeoArea>();
            }
        }

        public Favorites Store { get; private set; }

        public FavoriteStore()
        {
            filepath = String.Format("{0}favorites.xml", Global.Settings.Get("ApplicationDataPath") as string);
            this.Store = new Favorites();
            this.Load();
        }

        public void Load()
        {
            if (File.Exists(filepath))
            {
                try
                {
                    using (FileStream fs = new FileStream(filepath, FileMode.Open))
                    {
                        Store = (Favorites)(new XmlSerializer(typeof(Favorites)).Deserialize(fs));
                    }
                    this.Valid = true;
                }
                catch (InvalidOperationException x)
                {
                    this.Valid = false;
                    System.Diagnostics.Debug.WriteLine(x.Message);
                }
            }
            else
            {
                this.Valid = false;
            }
        }

        public void Save()
        {
            try
            {
                using (FileStream fs = new FileStream(filepath, FileMode.Create))
                {
                    new XmlSerializer(typeof(Favorites)).Serialize(fs, Store);
                }
            }
            catch (Exception x)
            {
                System.Windows.Forms.MessageBox.Show(x.Message);
            }
        }

        /* have no idea what was that... it's a leftover from the initial design
        public int UpdateLine(Service.LineInfo line)
        {
            int elementsRemoved = FavouriteLines.LineDetails.RemoveAll(delegate(Service.LineInfo ln) { return ln.lnId == line.lnId; });
            FavouriteLines.Meta.LineSummary.Add(line.lnId, line.lnNumber);
            FavouriteLines.LineDetails.Add(line);
            return elementsRemoved;
        }
        */
    }
}
